package yss.learn2develop.demoactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;


public class Main extends Activity {

    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Send button init
        Button _sendButton = (Button)findViewById(R.id.button_send);
        //添加事件监听！！
        _sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取输入的参数
                EditText _editText = (EditText)findViewById(R.id.edit_Text);
                String _message = _editText.getText().toString();
                //创建intent 传输参数
                Intent _intent = new Intent();

                _intent.setAction("yss.intent.GETMESSAGE");
                _intent.putExtra(EXTRA_MESSAGE,_message);

                //启动Activity！
                Main.this.startActivity(_intent);
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 按钮创建事件相应
     * @param view 被点击的按钮
     */
    public void createOnClick(View view){
        LinearLayout _ll = (LinearLayout)findViewById(R.id.layout_create);
        Button _newButton = new Button(this);
        _newButton.setText("退出");

        _ll.addView(_newButton, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

        _newButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Main.this.finish();
            }
        });
    }
}
